package com.sgb.member.weight;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.sgb.member.R;
import com.sgb.member.databinding.DialogMemberCommonBinding;

import java.util.Objects;

public class MemberCommonDialog extends DialogFragment {

    // 会员管理 会员调级 ApplyPcStyle
    public static final int STYLE_LEVEL = 1;
    // 会员管理 解除会员 DissolutionMemberStyle
    public static final int STYLE_DISSOLUTION = 2;
    // 会员管理 已关注  FollowStyle
    public static final int STYLE_FOLLOW = 3;
    // 申请加盟 ApplyJoinStyle
    public static final int STYLE_APPLY_JOIN = 4;
    // 邀请加盟
    public static final int STYLE_INVITE_JOIN = 5;
    // 确认发起审批当前业务？
    public static final int STYLE_CONFIRM_APPROVAL = 6;
    // 会员管理 解除会员申请
    public static final int STYLE_DISSOLUTION_APPLY = 7;
    // 首页发布 权限
    public static final int STYLE_PERMISSION = 8;

    private DialogMemberCommonBinding mBinding;
    private final MemberCommonListener listener;
    private final int style;

    public MemberCommonDialog(MemberCommonListener listener, int style) {
        this.listener = listener;
        this.style = style;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MemberDialogTheme);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = Objects.requireNonNull(this.getDialog()).getWindow();
        //去掉dialog默认的padding
        assert window != null;
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_member_common, container, false);
        mBinding.tvAction2.setOnClickListener(v -> {
            dismiss();
            if (listener != null) {
                listener.onNegative();
            }
        });
        mBinding.tvAction3.setOnClickListener(v -> {
            dismiss();
            if (listener != null) {
                listener.onPositive();
            }
        });

        switch (style){
            case STYLE_LEVEL:
                mBinding.tvAction1.setText("管理员已设置审批，您需要填写审批信息，审批通过后自动调级成功");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("去填写");
                break;
            case STYLE_DISSOLUTION:
                mBinding.tvAction1.setText("确认解除会员关系？");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("确定");
                break;
            case STYLE_DISSOLUTION_APPLY:
                mBinding.tvAction1.setText("管理员已设置审批，您需要填写审批信息，审批通过后自动解除成功");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("去填写");
                break;
            case STYLE_FOLLOW:
                mBinding.tvAction1.setText("确定要取消关注吗？");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("确定");
                break;
            case STYLE_APPLY_JOIN:
                mBinding.tvAction1.setText("管理员已设置审批，您需要填写审批信息，审批通过方能申请加盟");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("去填写");
                break;
            case STYLE_INVITE_JOIN:
                mBinding.tvAction1.setText("管理员已设置审批，您需要填写审批信息，审批通过方能邀请加盟");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("去填写");
                break;
            case STYLE_CONFIRM_APPROVAL:
                mBinding.tvAction1.setText("确认发起审批当前业务？");
                mBinding.tvAction2.setText("取消");
                mBinding.tvAction3.setText("确定");
                break;
            case STYLE_PERMISSION:
                mBinding.tvTrip.setText("您没有权限");
                mBinding.tvAction1.setText("请把身份切换到企业或者加入已认证的企业后再试");
                mBinding.tvAction2.setText("我知道了");
                mBinding.tvAction3.setText("切换身份");
                break;
        }
        return mBinding.getRoot();
    }

    /**
     * 显示Dialog
     *
     * @param tag 设置一个标签用来标记Dialog
     */
    public void show(AppCompatActivity activity, String tag) {
        show(activity, null, tag);
    }

    public void show(Context context, String tag) {
        show((AppCompatActivity) context, null, tag);
    }

    /**
     * 显示Dialog
     *
     * @param bundle 要传递给Dialog的Bundle对象
     * @param tag    设置一个标签用来标记Dialog
     */
    public void show(AppCompatActivity activity, Bundle bundle, String tag) {
        if (activity == null && isShowing()) {
            return;
        }
        assert activity != null;
        FragmentTransaction mTransaction = activity.getSupportFragmentManager().beginTransaction();
        Fragment mFragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (mFragment != null) {
            //为了不重复显示dialog，在显示对话框之前移除正在显示的对话框
            mTransaction.remove(mFragment);
        }
        if (bundle != null) {
            setArguments(bundle);
        }
        show(mTransaction, tag);
    }

    /**
     * 是否显示
     *
     * @return false:isHidden  true:isShowing
     */
    protected boolean isShowing() {
        if (this.getDialog() != null) {
            return this.getDialog().isShowing();
        } else {
            return false;
        }
    }

    public static class SimpleListener implements MemberCommonListener {

        @Override
        public void onPositive(Object... objects) {

        }

        @Override
        public void onNegative() {

        }
    }

    private interface MemberCommonListener {
        void onPositive(Object... objects);

        void onNegative();
    }

}
